package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.mybeans.dao.DAOException;

import databean.FundBean;
import databean.FundPriceHistoryBean;

import model.FundDAO;
import model.FundPriceHistoryDAO;
import model.Model;

public class CustomerResearchFundAction extends Action{

	

	private FundDAO     fundDAO;
	private FundPriceHistoryDAO		fundPriceHistoryDAO;
	
	
	@Override
	public String getName() {
		return "customer-research-fund.do";
	}
    
	
	public CustomerResearchFundAction(Model model){
		fundPriceHistoryDAO = model.getFundPriceHistoryDAO();
		fundDAO = model.getFundDAO();
	}
	
	
	@Override
	public String perform(HttpServletRequest request,
			HttpServletResponse response) {
		
		
		request.setAttribute("fundID", request.getParameter("fundID"));
		
		// Set up the errors list
        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        
        //returns the existing session or null, if the session does not exist.
        HttpSession session = request.getSession(false);
        
		try {
	        
			FundPriceHistoryBean[] fundPriceList = fundPriceHistoryDAO.getFundPriceHistory();
	        request.setAttribute("fundPriceList",fundPriceList);

	        FundBean[] newFundList = fundDAO.getFunds();
	        session.setAttribute("fundList",newFundList);
	                
	        // After successful registration (and login) send to...
	        String redirectTo = (String) session.getAttribute("redirectTo");
	        if (redirectTo != null) return redirectTo;

			return "customer-research-fund.jsp";
        } catch (DAOException e) {
        	errors.add(e.getMessage());
        	return "employee-research-fund.jsp";
        }
	}

}
